﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.DynamicData;
using NotAClue.ComponentModel.DataAnnotations;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace NotAClue.Web.DynamicData
{
    public static class FilterExtensionMethods
    {
        /// <summary>
        /// Populates the list control filtered attribute in meta data.
        /// </summary>
        /// <param name="filter">The field template.</param>
        /// <param name="listControl">The list control.</param>
        /// <remarks></remarks>
        public static void PopulateListControlFiltered(this QueryableFilterUserControl filter, MetaForeignKeyColumn column, ListControl listControl)
        {
            var parentTable = column.ParentTable;
            var items = column.ParentTable.GetQuery().ApplyColumnFilter(parentTable);
            foreach (object obj2 in items)
            {
                string displayString = parentTable.GetDisplayString(obj2);
                string primaryKeyString = parentTable.GetPrimaryKeyString(obj2);
                listControl.Items.Add(new ListItem(displayString, primaryKeyString.TrimEnd(new char[0])));
            }
        }

        /// <summary>
        /// Sets the filter visibility.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="column">The column.</param>
        /// <remarks></remarks>
        public static void SetFilterVisibility(this QueryableFilterUserControl filter, MetaColumn column)
        {
            var filterAttribute = column.GetAttributeOrDefault<FilterAttribute>();
            var roles = Roles.GetRolesForUser();

            if (filterAttribute.Hidden)
                filter.Visible = false;
            else
            {
                if (filterAttribute.HiddenForRoles != null && filterAttribute.HiddenForRoles.Length > 0 && roles.HasAnyRole(filterAttribute.HiddenForRoles))
                    filter.Visible = false;
            }
        }

        /// <summary>
        /// Sets the filter default value.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="column">The column.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public static String SetFilterDefaultValue(this QueryableFilterUserControl filter, MetaColumn column)
        {
            var filterAttribute = column.GetAttributeOrDefault<FilterAttribute>();

            if (!String.IsNullOrEmpty(filterAttribute.DefaultValue) && String.IsNullOrEmpty(filter.DefaultValue))
                return filterAttribute.DefaultValue;

            return filter.DefaultValue;
        }

        /// <summary>
        /// Sets the field options.
        /// </summary>
        /// <param name="filter">The control.</param>
        /// <param name="column">The column.</param>
        public static void SetUpFilter(this QueryableFilterUserControl filter, MetaColumn column)
        {
            //var filterAttribute = column.GetAttributeOrDefault<FilterAttribute>();
            var Label1 = filter.FindControlRecursive<Label>("Label1");
            if (Label1 != null)
            {
                //if (!String.IsNullOrEmpty(filterAttribute.DisplayName))
                //    Label1.Text = filterAttribute.DisplayName;
                //else
                Label1.Text = column.DisplayName;
            }
        }
    }
}
